using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BlueFinity.mvNET.CoreObjects;
using mvLibrary.Helpers.Utils;

namespace fenk.Proveedores
{
    public partial class PrFacturas : mvLibrary.BaseForm
    {
        public PrFacturas()
        {
            InitializeComponent();
        }
        // APLIC.DATA
        // 4 = descripcion volante (agregado julio 2010)
        // 5 = descripcion productos MV
        //
        public override void BeforeSaveItem()
        {
            if (this.txtVolante.Text == "0" && !FenkUtils.TienePermisosDeEdicion(User))
            {
                CancelSaveItem = true;
                MessageBoxCreator.ErrorMessage("Debe tener permisos para no ingresar un volante",
                                               "Error Volante obligatorio");
            }
            if (ItemStatus == Status.New)
            {
                UniItem.Replace(73, User);
            }
            //
            // hay que recalcular totales por producto?
            //
            if (UniItem.ExtractAsString(20) != string.Empty)
            {
                string totalproductos = "";
                int countProductos = UniItem.Dcount(20);
                for (int a = 1; a <= countProductos; ++a)
                {
                    totalproductos = GetDefaultValue("@CALC D24 * D22", false, 1, a);
                    if (totalproductos != UniItem.ExtractAsString(26, a))
                    {
                        CancelSaveItem = true;
                        ErrorMessage = "Debe recalcular valores en la fila " + a.ToString() + " de productos";
                        break;
                    }
                }
            }
            int totalImpo, totalAsiento, totalStock;
            #region Total importaciones
            try
            {
                totalImpo = int.Parse(GetDefaultValue("@CALC T28"));
                totalAsiento = int.Parse(GetDefaultValue("D37"));
            }
            catch
            {
                totalImpo = 0;
                totalAsiento = 0;
            }
            if (totalImpo > 0 & totalImpo != totalAsiento)
            {
                CancelSaveItem = true;
                ErrorMessage = "Total Importaciones difiere con total asiento";
                return;
            }
            #endregion
            #region Total Stock
            try
            {
                totalStock = int.Parse(GetDefaultValue("@CALC D81 + D79"));
            }
            catch
            {
                totalStock = 0;
            }
            if (totalStock > 0 && (totalAsiento - totalStock) > 300)
            {
                if (MessageBox.Show("Total stock difiere con total comprobante. Continuar?", "stock", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                {
                    CancelSaveItem = true;
                    ErrorMessage = "Revise total stock";
                }
            }
            #endregion
            #region Asiento balancea?
            long saldo;
            try
            {
                saldo = long.Parse(GetDefaultValue("@CALC T62"));
            }
            catch
            {
                saldo = 1;
            }
            if (saldo != 0)
            {
                CancelSaveItem = true;
                ErrorMessage = "Asiento no balancea";
            }
            #endregion
            #region Totales Iva, Percepcion x contable igual a totales comprobante
            long iva = 0, percepcion = 0, importe = 0, totalContable = 0, comprobante = 0;
            
            try
            {
                comprobante = long.Parse(UniItem.ExtractAsString(39));
            }
            catch
            {
                CancelSaveItem = true;
                ErrorMessage = "Total del comprobante no es un importe valido";
                return;
            }
            for (int a = 1; a <= UniItem.Dcount(56); ++a)
            {
                try
                {
                    importe = long.Parse(UniItem.ExtractAsString(62, a));
                }
                catch
                {
                    importe = 0;
                }

                if (comprobante > 0)
                {
                    if (importe > 0) totalContable = totalContable + importe;
                }
                else
                {
                    if (importe < 0) totalContable = totalContable + importe;
                }

                if (UniItem.ExtractAsString(56, a) == "IVA")
                {
                    iva = long.Parse(UniItem.ExtractAsString(62, a));
                }
                if (UniItem.ExtractAsString(56, a) == "RPP")
                {
                    percepcion = long.Parse(UniItem.ExtractAsString(62, a));
                }
            }
            if (totalContable != comprobante)
            {
                CancelSaveItem = true;
                ErrorMessage = "Asiento contable no coincide con total comprobante ";
            }
            if (iva.ToString() != UniItem.ExtractAsString(38))
            {
                CancelSaveItem = true;
                ErrorMessage = "Total IVA contable difiere con total comprobante";
            }
            if (UniItem.ExtractAsString(40)!=string.Empty &  percepcion.ToString() != UniItem.ExtractAsString(40))
            {
                CancelSaveItem = true;
                ErrorMessage = "Total Percepcion contable difiere con total comprobante";
            }
            #endregion
        }

        private void buttonFF_Click(object sender, EventArgs e)
        {
            // asiento default fondo fijo
            if (ItemStatus!= Status.New || UniItem.ExtractAsString(1) != "F*100") return;
            CallProgram("PR.READ.FF", 1, 1);
            gridContable.Rows.Clear();
            FillDataGridView(ref gridContable);
        }

        private void buttonDC_Click(object sender, EventArgs e)
        {
            // asiento default compras
            if (ItemStatus == Status.New)
            {
                UniItem.Replace(56,"COM" + DataBASIC.VM + "IVA");
                UniItem.Replace(58, string.Empty+ DataBASIC.VM + string.Empty);
                UniItem.Replace(61, "114*020*00001" + DataBASIC.VM + "113*210*00001");
                UniItem.Replace(62, UniItem.ExtractAsString(37) + DataBASIC.VM + UniItem.ExtractAsString(38));
                gridContable.Rows.Clear();
                FillDataGridView(ref gridContable);
                gridContable.Focus();
                gridContable.AddNewRow();
            }
        }

        private void buttonDP_Click(object sender, EventArgs e)
        {
            // asiento default pedido especial
            if (ItemStatus == Status.New)
            {
                UniItem.Replace(56, "PED" + DataBASIC.VM + "IVA");
                UniItem.Replace(58, string.Empty + DataBASIC.VM + string.Empty);
                UniItem.Replace(61, "114*020*00001" + DataBASIC.VM + "113*210*00001");
                UniItem.Replace(62, UniItem.ExtractAsString(37) + DataBASIC.VM + UniItem.ExtractAsString(38));
                gridContable.Rows.Clear();
                FillDataGridView(ref gridContable);
                gridContable.Focus();
                gridContable.AddNewRow();
          }
        }

        private void butODC_Click(object sender, EventArgs e)
        {
            if (ItemStatus == Status.New)
            {
                if (MessageBox.Show("Cargar detalle productos desde ODC?", "Confirmar", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    CallProgram("PR.LOAD.ODC", 1, 1);
                    gridStock.Rows.Clear();
                    FillDataGridView(ref gridStock);
                    if (gridStock.Rows.Count > 0)
                    {
                        gridStock.CurrentCell = gridStock[0, 0];
                    }
                }
            }
        }

        private void butRT_Click(object sender, EventArgs e)
        {
            // asiento default fondo fijo
            if (ItemStatus != Status.New || UniItem.ExtractAsString(1) != "T*00") return;
            CallProgram("PR.READ.RT", 1, 1);
            gridContable.Rows.Clear();
            FillDataGridView(ref gridContable);
        }

        
    }
}

